package com.fable.web.controller.customer;

import com.fable.biz.operate.ContactsService;
import com.fable.biz.operate.CustomerService;
import com.fable.biz.operate.dto.CustomerDTO;
import com.fable.common.config.RequireLogin;
import com.fable.common.enums.BusiErrCode;
import com.fable.common.utils.JsonResult;
import com.fable.dal.condition.ContactsQueryCondition;
import com.fable.dal.dto.ContactsDTO;
import com.fable.dal.model.ContactsDO;
import com.fable.utils.StringUtil;
import com.fable.web.common.admin.UserTools;
import com.fable.web.page.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 客户小计
 *
 * @ClassName ContactsController
 * @Description TODO
 * @Author aaters
 * @Date 2018/8/12 下午1:45
 * @Version 1.0
 **/

@Controller
@RequireLogin
@RequestMapping("/contacts")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @Autowired
    private CustomerService customerService;


    @RequestMapping("/finAll.htm")
    public String findContactsAll(ModelMap modelMap, ContactsQueryCondition condition) {
        List<ContactsDTO> result = contactsService.findContactsAll(condition);
        modelMap.put("contacts", result);
        modelMap.put("query", condition);
        return P.Common.contactsList;
    }


    @RequestMapping("contactsAdd.htm")
    public String contactsAddHtm(ModelMap modelMap, Integer id) {
        ContactsDO contactsDO = contactsService.findContactsById(id);
        List<CustomerDTO> customerList = customerService.findCustomerList(null);
        modelMap.put("contactsDO", contactsDO);
        modelMap.put("customer", customerList);
        return P.Common.contactsAdd;

    }


    @RequestMapping("contactsAdd.json")
    @ResponseBody
    public JsonResult contactsAddJson(@ModelAttribute ContactsDO contactsDO,HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        if (contactsDO == null) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
        }
        Long loginId = UserTools.getLoginId(session);
        contactsDO.setAdminId(loginId.compareTo(loginId));
        int count = contactsService.addOrModifyContacts(contactsDO);
        if (count == 0) {
            jsonResult.setErrCode(BusiErrCode.SERVER_BUSY);
        }
        return jsonResult;

    }


    /**
     * 逻辑删除客户小计
     *
     * @return
     */
    @RequestMapping("/delContacts.json")
    @ResponseBody
    public JsonResult delCustomer(String id) {
        JsonResult jsonResult = new JsonResult();
        if (StringUtil.isEmail(id)) {
            jsonResult.setErrCode(BusiErrCode.PARAM_ERROR);
            return jsonResult;
        }
        ContactsDO contactsDO = new ContactsDO();
        contactsDO.setId(Integer.valueOf(id));
        contactsDO.setStatus("-1");
        contactsDO.setGmtModified(new Date());
        int count = contactsService.delContacts(contactsDO);
        if (count != 1) {
            jsonResult.setErrCode(BusiErrCode.INNER_ERROR);
        }
        return jsonResult;
    }
}



